package com.tripwego.client.ui.widget;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tripwego.client.common.TwgConstants;
import com.tripwego.client.event.AddActiviteEvent;
import com.tripwego.client.event.AddActiviteEventHandler;
import com.tripwego.client.event.AddLodgingCheckoutEvent;
import com.tripwego.client.event.AddLodgingCheckoutEventHandler;
import com.tripwego.client.event.AddRentalCheckinEvent;
import com.tripwego.client.event.AddRentalCheckinEventHandler;
import com.tripwego.client.event.DeleteLodgingEvent;
import com.tripwego.client.event.DeleteLodgingEventHandler;
import com.tripwego.client.model.ActiviteModel;
import com.tripwego.client.model.DayModel;
import com.tripwego.client.ui.pageVoyage.IVoyageView;
import com.tripwego.client.ui.widget.activite.ActiviteWidget;
import com.tripwego.client.ui.widget.activite.LodgingCheckinWidget;
import com.tripwego.client.ui.widget.activite.LodgingCheckoutWidget;
import com.tripwego.client.ui.widget.activite.LodgingNightWidget;
import com.tripwego.client.ui.widget.activite.LodgingWidget;
import com.tripwego.client.ui.widget.activite.RentalCheckinWidget;

public class DayContentWidget extends Composite implements IVoyageView, AddActiviteEventHandler, AddLodgingCheckoutEventHandler, AddRentalCheckinEventHandler,
		DeleteLodgingEventHandler {

	private Presenter presenter;

	private EventBus eventBus;

	private DayWidget dayWidget;

	private DayModel dayModel;

	/*
	 * Elements graphiques
	 */

	private VerticalPanel viewPanel = new VerticalPanel();

	private VerticalPanel panel_listActivite = new VerticalPanel();

	public DayContentWidget(DayWidget dayWidget) {
		super();

		viewPanel.add(panel_listActivite);

		initWidget(viewPanel);

		this.dayWidget = dayWidget;
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;

		// ecouteurs d'evenements
		eventBus.addHandler(AddActiviteEvent.TYPE, this);
		// eventBus.addHandler(AddLodgingCheckinEvent.TYPE, this);
		eventBus.addHandler(AddLodgingCheckoutEvent.TYPE, this);
		eventBus.addHandler(AddRentalCheckinEvent.TYPE, this);
		eventBus.addHandler(DeleteLodgingEvent.TYPE, this);
	}

	@Override
	public void init() {
		// TODO
	}

	@Override
	public void handleAddActiviteEvent(AddActiviteEvent event) {

		System.out.println("handleAddActiviteEvent");

		ActiviteModel activiteModel = event.getActiviteModel();

		if (activiteModel.getListDaySelectionne().contains(dayModel.getDate())) {

			if (TwgConstants.TYPE_ACTV_LODGING.equals(activiteModel.getType())) {
				// checkin
				if (activiteModel.getDateDeb().equals(dayModel.getDate())) {

					LodgingCheckinWidget lodgingCheckinWidget = new LodgingCheckinWidget(activiteModel);
					lodgingCheckinWidget.setWidth("350px");
					lodgingCheckinWidget.setPresenter(presenter);
					lodgingCheckinWidget.setEventBus(eventBus);
					lodgingCheckinWidget.init();

					viewPanel.add(lodgingCheckinWidget);
				}
				// nuit
				else {
					LodgingNightWidget lodgingNightWidget = new LodgingNightWidget(dayWidget, activiteModel);
					lodgingNightWidget.setWidth("350px");
					lodgingNightWidget.setPresenter(presenter);
					lodgingNightWidget.setEventBus(eventBus);
					lodgingNightWidget.init();

					viewPanel.add(lodgingNightWidget);
				}
			} else {
				ActiviteWidget activiteWidget = new ActiviteWidget(activiteModel);
				activiteWidget.setWidth("350px");
				activiteWidget.setPresenter(presenter);
				activiteWidget.setEventBus(eventBus);
				activiteWidget.init();

				panel_listActivite.add(activiteWidget);
			}

			dayWidget.open(true);
		}
	}

	/**
	 * @return the dayModel
	 */
	public DayModel getDayModel() {
		return dayModel;
	}

	/**
	 * @param dayModel
	 *            the dayModel to set
	 */
	public void setDayModel(DayModel dayModel) {
		this.dayModel = dayModel;
	}

	// @Override
	// public void addLodgingCheckinWidget(Activ) {
	// System.out.println("handleAddLodgingCheckinEvent");
	//
	// ActiviteModel activiteModel = event.getActiviteModel();
	//
	// if (activiteModel.getDateDeb().equals(dayModel.getDate())) {
	//
	// }
	// }

	@Override
	public void handleAddLodgingCheckoutEvent(AddLodgingCheckoutEvent event) {
		System.out.println("handleAddLodgingCheckoutEvent");

		ActiviteModel activiteModel = event.getActiviteModel();

		if (activiteModel.getDateFin().equals(dayModel.getDate())) {
			LodgingCheckoutWidget lodgingCheckoutWidget = new LodgingCheckoutWidget(activiteModel);
			lodgingCheckoutWidget.setWidth("350px");
			lodgingCheckoutWidget.setPresenter(presenter);
			lodgingCheckoutWidget.setEventBus(eventBus);
			lodgingCheckoutWidget.init();

			viewPanel.insert(lodgingCheckoutWidget, 0);
			dayWidget.open(true);
		}
	}

	@Override
	public void handleAddRentalCheckinEvent(AddRentalCheckinEvent event) {
		System.out.println("handleAddRentalCheckinEvent");

		ActiviteModel activiteModel = event.getActiviteModel();

		if (activiteModel.getDateDeb().equals(dayModel.getDate())) {
			RentalCheckinWidget rentalCheckinWidget = new RentalCheckinWidget(dayWidget, activiteModel);
			rentalCheckinWidget.setWidth("350px");
			rentalCheckinWidget.setPresenter(presenter);
			rentalCheckinWidget.setEventBus(eventBus);
			rentalCheckinWidget.init();

			panel_listActivite.add(rentalCheckinWidget);
			dayWidget.open(true);
		}
	}

	@Override
	public void handleDeleteLodgingEvent(DeleteLodgingEvent event) {
		System.out.println("handleDeleteLodgingEvent");

		ActiviteModel activiteModelASuppr = event.getActiviteModel();
		Long idTemp = null;

		if (activiteModelASuppr.getListDaySelectionne().contains(dayModel.getDate()) || activiteModelASuppr.getDateFin().equals(dayModel.getDate())) {

			for (int i = 0; i < viewPanel.getWidgetCount(); i++) {

				if (viewPanel.getWidget(i) instanceof LodgingCheckinWidget) {
					LodgingCheckinWidget lodgingCheckinWidget = (LodgingCheckinWidget) viewPanel.getWidget(i);
					idTemp = lodgingCheckinWidget.getActiviteModel().getId();
				}
				if (viewPanel.getWidget(i) instanceof LodgingNightWidget) {
					LodgingNightWidget lodgingNightWidget = (LodgingNightWidget) viewPanel.getWidget(i);
					idTemp = lodgingNightWidget.getActiviteModel().getId();
				}
				if (viewPanel.getWidget(i) instanceof LodgingCheckoutWidget) {
					LodgingCheckoutWidget lodgingCheckoutWidget = (LodgingCheckoutWidget) viewPanel.getWidget(i);
					idTemp = lodgingCheckoutWidget.getActiviteModel().getId();
				}

				if (idTemp != null && idTemp.equals(activiteModelASuppr.getId())) {
					viewPanel.remove(i);
				}
			}
		}
	}
}
